package com.ccl.iot.views;import android.content.Context;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ListView;

public class HorizontalScrollListView extends HorizontalScrollView {
	private boolean iChildDowned = false;
	
	public HorizontalScrollListView(Context context) {
		super(context);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		int tCount = this.getChildCount();
    	
        for (int i = 0; i < tCount; i++) {
            this.getChildAt(i).layout(l, t, r, b);
        }
	}
	
	private boolean hasListChildFocused(ViewGroup aView){
		if (aView != null){
			int tCount = aView.getChildCount();
			
			for (int i = 0; i < tCount; i++){
				Object tChild = aView.getChildAt(i);
				
				if (tChild != null){
					if (tChild instanceof ListView){
						if (((ListView) tChild).isFocused() || ((ListView) tChild).getFocusedChild() != null){
							return true;
						}
					}else if (tChild instanceof ViewGroup){
						if (hasListChildFocused((ViewGroup) tChild)){
							return true;
						}
					}
				}
			}
		}
		
		return false;
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		switch (ev.getAction()){
			case MotionEvent.ACTION_DOWN:{
				if (hasListChildFocused(this)){
					iChildDowned = true;
					
					return false;
				}
				
				break;
			}
			case MotionEvent.ACTION_MOVE:{
				if (iChildDowned){
					return false;
				}
			}		
			case MotionEvent.ACTION_UP:{
				iChildDowned = false;
				
				break;
			}
		}
		
		return super.onInterceptTouchEvent(ev);
	}
}
